package com.lty.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Slf4j
@Configuration
public class RedisMessageListenerConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("redis:topic"));
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(RedisReceiverListener redisReceiverListener) {
        return new MessageListenerAdapter(redisReceiverListener);
    }

    @Bean
    public RedisReceiverListener redisReceiver() {
        return new RedisReceiverListener();
    }
}
